package com.twu.biblioteca;

import java.io.InputStream;
import java.util.Scanner;
import java.util.Arrays;


public class Library {

    public static final String STARTUP_MESSAGE =
            "Welcome to the Biblioteca Library Management System!\r\nPlease select an option:";
    public static final String[] OPTIONS = {
            "View Book List",
            "View Movie List",
            "Login",
            "Reserve Book",
            "Check Membership Details"
    };

    public static final String INPUT_CONFIRMATION = "You have selected: ";
    public static final String WARNING = "Select a valid option!!";
    public static final String RESERVE_SUCCESSFUL = "Thank You! Enjoy the book.";
    public static final String RESERVE_FAILED = "Sorry we don't have that book yet.";
    public static final String MEMBERSHIP_MESSAGE = "Please talk to a Librarian. Thank you.";
    public static final String RESERVE_INSTRUCTIONS = "Which book would you like to reserve?";
    public static final String MOVIE_COLUMNS = "Movie Year Director Rating";
    public static final String LOGIN_INSTRUCTIONS = "Please enter your username:";
    public static final String PASSWORD_PROMPT = "Password:";
    public static final String INVALID_LOGIN = "Username/Password invalid.";
    public static final String SUCCESSFUL_LOGIN = "Success! You are now logged in.";
    public static final String INVALID_ACTION = "Sorry, you must be logged in to user this feature.";

    private String[] bookList = {
            "The Jungle Book",
            "1984",
            "A Midsummer Night's Dream"
    };

    private Movie[] movieList = {
            new Movie("Forest Gump", 1994, "Robert Zemeckis", "9"),
            new Movie("Grave of the Fireflies", 1988, "Isao Takahata", "8"),
            new Movie("Princess Mononoke", 1997, "Hayao Miyazaki", "8"),
            new Movie("District 9", 2004, "Neill Blomkamp", "8"),
            new Movie("WALL-E", 2008, "Andrew Stanton", "9"),
            new Movie("Serenity", 2005, "Joss Whedon", "8"),
            new Movie("Little Miss Sunshine", 2006, "Jonathan Dayton", "8"),
            new Movie("Fight Club", 1999, "David Fincher", "9"),
            new Movie("Star Trek", 2009, "j.j. Abrams", "8"),
            new Movie("I Am Sam", 2001, "Jessie Nelson", "7"),
            new Movie("Lock, Stock & Two Smoking Barrels", 1998, "uy Ritchie", "8"),
            new Movie("V for Vendetta", 2005, "James McTeigue", "8"),
            new Movie("The Room", 2003, "Tommy Wiseau", "3"),
            new Movie("Sholay", 1975, "Ramesh Sippy", "N/A"),
            new Movie("Donnie Darko", 2001, "Richard Kelly", "8")
    };

    Scanner scanner;
    String currentUser = "";

    public Library(InputStream input){
        scanner = new Scanner(input);
        System.out.println(STARTUP_MESSAGE);
        for (String line : OPTIONS) {
            System.out.println(line);
        }
        handleUserInput();
    }

    private void handleUserInput() {
        while(scanner.hasNextLine()){
            String selectedOption = scanner.nextLine();
            if(selectedOption.equalsIgnoreCase(OPTIONS[0])) {
                displayBookList();
            } else if(selectedOption.equalsIgnoreCase(OPTIONS[1])) {
                displayMovieList();
            } else if(selectedOption.equalsIgnoreCase(OPTIONS[2])) {
                login();
            } else if(selectedOption.equalsIgnoreCase(OPTIONS[3])) {
                reserveBook();
            } else if(selectedOption.equalsIgnoreCase(OPTIONS[4])) {
                viewMembershipDetails();
            } else {
                System.out.println(WARNING);
            }
        }
    }

    private void displayBookList() {
        System.out.println(INPUT_CONFIRMATION + OPTIONS[0]);
        for (String book : bookList) {
            System.out.println(book);
        }
    }

    private void displayMovieList() {
        System.out.println(INPUT_CONFIRMATION + OPTIONS[1]);
        System.out.println(MOVIE_COLUMNS);

        for (Movie movie : movieList) {
            System.out.println(movie.getFormattedDetails());
        }
    }

    private void login() {
        System.out.println(INPUT_CONFIRMATION + OPTIONS[2]);
        System.out.println(LOGIN_INSTRUCTIONS);

        String username = null;
        String password = null;
        if(scanner.hasNextLine()){
            username = scanner.nextLine();
        }
        System.out.println(PASSWORD_PROMPT);
        if(scanner.hasNextLine()){
            password = scanner.nextLine();
        }

        if(Login.getInstance().checkLogin(username, password)) {
            System.out.println(SUCCESSFUL_LOGIN);
            currentUser = username;
        } else {
            System.out.println(INVALID_LOGIN);
        }
    }

    private void reserveBook() {
        System.out.println(INPUT_CONFIRMATION + OPTIONS[3]);
        if(!currentUser.isEmpty()) {
            System.out.println(RESERVE_INSTRUCTIONS);

            String selectedBook = null;
            if(scanner.hasNextLine()){
                selectedBook = scanner.nextLine();
            }

            if(Arrays.asList(bookList).contains(selectedBook)) {
                System.out.println(RESERVE_SUCCESSFUL);
            } else {
                System.out.println(RESERVE_FAILED);
            }
        } else {
            System.out.println(INVALID_ACTION);
        }
    }

    private void viewMembershipDetails() {
        System.out.println(INPUT_CONFIRMATION + OPTIONS[4]);
        if(!currentUser.isEmpty()) {
            System.out.println(Login.getInstance().getUserDetails(currentUser));
        } else {
            System.out.println(MEMBERSHIP_MESSAGE);
        }
    }
}
